/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pv168.reminder.swing.workers;

import cz.muni.fi.pv168.reminder.entities.Task;
import cz.muni.fi.pv168.reminder.managers.TaskManager;
import cz.muni.fi.pv168.reminder.managers.implementation.TaskManagerImpl;
import cz.muni.fi.pv168.reminder.swing.FrameMainWindow;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;

/**
 *
 * @author Adic
 */
public class LoadTasksSwingWorker extends SwingWorker<List<Task>, Void> {
    private Boolean all, done, pending, over;
    
    public LoadTasksSwingWorker() {
        all = Boolean.TRUE;
        done = Boolean.FALSE;
        pending = Boolean.FALSE;
        over = Boolean.FALSE;
    }

    public LoadTasksSwingWorker(Boolean all, Boolean done, Boolean pending, Boolean over) {
        this.all = all;
        this.done = done;
        this.pending = pending;
        this.over = over;
    }

    @Override
    protected List<Task> doInBackground() throws Exception {
        List<Task> tasks = new ArrayList<>();
        TaskManager taskManager = new TaskManagerImpl();
        taskManager.setDataSource(FrameMainWindow.getDataSource());
        
        if(all){
            tasks = taskManager.findAllTasks();
            return tasks;
        }
        
        if(done){
            tasks = unionLists(tasks, taskManager.findAllDoneTasks());
        }
        if(pending){
            tasks = unionLists(tasks, taskManager.findAllPendingTasks());
        }
        if(over){
            tasks = unionLists(tasks, taskManager.findAllTasksOverDeadline());
        }
        
        
        
        
        return tasks;
    }

    /**
     * Create union of two lists.
     * @param union1
     * @param union2
     * @return 
     */
    private List<Task> unionLists(List<Task> union1, List<Task> union2) {
        for(Task task:union2){
            if(!union1.contains(task)){
                union1.add(task);
            }
        }
        return union1;
    }
    
}
